package com.zm.blog.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import java.time.LocalDateTime;

/**
 * @Author: Sister Xiao Ming
 * @Description: UserVO类，作为用户信息的视图对象（Value Object），混合了多个关注点（如认证信息、用户基本信息、时间信息等）
 */
@Data
public class UserVO {

    private Integer id;

    @NotBlank(message = "用户名不能为空")
    private String username;

    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式错误")
    private String phoneNumber;

    @Email(message = "邮箱格式错误")
    private String email;

    // 密码字段：只在创建/更新时使用，返回时自动置空
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotBlank(message = "密码不能为空", groups = {CreateGroup.class, UpdatePasswordGroup.class})
    @Pattern(regexp = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,20}$",
            message = "密码需8-20位字母+数字组合",
            groups = {CreateGroup.class, UpdatePasswordGroup.class})
    private String password;

    // 性别：0-未知 1-男 2-女
    private Integer gender = 0;

    //头像
    private String avatar;

    //简介
    private String introduction;

    //订阅
    private String subscribe;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    //最终修改人
    private String updateBy;

    // 使用标准布尔命名
    private Boolean isBoss = false;

    // 访问令牌：只在登录响应时使用
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String accessToken;

    // 验证码：只在注册/登录时使用
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String code;

    // 验证分组
    public interface CreateGroup {}
    public interface UpdateGroup {}
    public interface UpdatePasswordGroup {}

    // 自动清理敏感字段的方法
    public UserVO cleanSensitiveData() {
        this.password = null;
        this.code = null;
        return this;
    }

}
